import { createRouter, createWebHistory } from "vue-router";
import Login from "@/views/login/login.vue";
import register from "@/views/register/register.vue";
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/login",
      name: "login",
      component: Login,
      meta:{title:'登录'}
    },
    {
      path: "/register",
      name: "register",
      component: register,
      meta:{title:'注册'}
    },
    {
      path: "/User/patient",
      name: "patient",
      component: ()=> import("@/views/User/patient.vue"),
      meta:{title:'家庭档案'}
    },
    {
      path: "/User/addPatient",
      name: "addPatient",
      component: ()=> import("@/views/User/addPatient.vue"),
      meta:{title:'添加患者'}
    },
    {
      path: '/consult/fast',
      component: () => import('@/views/Consult/ConsultFast.vue'),
      meta: { title: '极速问诊' }
    },
    {
      path: '/consult/dep',
      component: () => import('@/views/Consult/ConsultDep.vue'),
      meta: { title: '选择科室' }
    },
    {
      path: '/consult/illness',
      component: () => import('@/views/Consult/ConsultIllness.vue'),
      meta: { title: '病情描述' }
    },
    {
      path: '/consult/pay',
      component: () => import('@/views/Consult/ConsultPay.vue'),
      meta: { title: '问诊支付' }
    },
    {
      path: '/room',
      component: () => import('@/views/Room/index.vue'),
      meta: { title: '问诊室' },
      beforeEnter(to) {
        if (to.query.payResult === 'false') return '/user/consult'
      }
    },
    {
      path: '/user/consult',
      component: () => import('@/views/User/ConsultPage.vue'),
      meta: { title: '问诊记录' }
    },
    {
      path: "/",
      name: "",
      component: () => import("@/views/layout/index.vue"),
      children: [
        {
          path: "/home",
          component: () => import("@/views/home/index.vue"),
          meta:{title:'首页'}
        },
        {
          path: "/atrcile",
          component: () => import("@/views/Article/index.vue"),
          meta:{title:'健康百科'}
        },
        {
          path: "/notify",
          component: () => import("@/views/Notify/index.vue"),
          meta:{title:'消息中心'}
        },
        {
          path: "/user",
          component: () => import("@/views/User/index.vue"),
          meta:{title:'个人中心'}
        },
      ],
    },
    // {
    //   path: '/about',
    //   name: 'about',
    //   // route level code-splitting
    //   // this generates a separate chunk (About.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () => import('../views/AboutView.vue')
    // }
  ],
});
import { useUserStore } from "@/stores";
router.beforeEach((to, from) => {
  const store = useUserStore();
  const whiteList = ["/login", "/register"];
  if (!store.user?.token && !whiteList.includes(to.path)) return "/login";
});
router.afterEach((to)=>{
  document.title=`${to.meta.title||''}-优医问诊`
})
export default router;
